﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mimoza.Apps.Console.ViewModel;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.Resources;

namespace Mimoza.Apps.Console.Commands
{
    public class DeleteContainerCommand : BaseConsoleCommand
    {
        ContainerTreeViewModel m_viewModel = null;
        public DeleteContainerCommand(ContainerTreeViewModel viewModel) 
        {
            m_viewModel = viewModel;
            m_viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.view_PropertyChanged);
        }

        void view_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            bool visible = false;
            bool enabled = false;

            Container cont = m_viewModel.Context as Container;

            if (null != cont)
            {

                if (m_viewModel.InFocus && cont.Type == ContainerType.Managed)
                {
                    visible = true;
                }

                if (!cont.IsSystem)
                {
                    enabled = true;
                }
                else
                {
                    enabled = false;
                }
            }
            
            this.Visible = visible;
            this.Enabled = enabled;
        }

        public override void Execute()
        {
            Container cont = m_viewModel.Context as Container;
            if (cont != null)
            {
                foreach ( Container c in cont.GetChilds())
                {
                    MessageBox.Show(Resource.Command_DeleteContainer_Error_ContNotEmpty, Resource.Command_DeleteContainer_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                foreach (Host h in cont.GetHosts())
                {
                    MessageBox.Show(Resource.Command_DeleteContainer_Error_HostInCont, Resource.Command_DeleteContainer_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                foreach (Policy p in cont.GetDirectPolicy())
                {
                    MessageBox.Show(Resource.Command_DeleteContainer_Error_PolicyInCont, Resource.Command_DeleteContainer_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                m_viewModel.Application.ObjectLayer.ContainerRepository.Delete(cont);
            }
        }
    }
}
